package jvDeformation;

import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.imageio.ImageIO;

public class JvGridLoader extends FileDialog{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4367917151349653937L;
	private int numGridPointsX =10;
	private  int numGridPointsY =10;
	private String name;
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	private final static String TITLE ="choose image";
	public JvGridLoader(Frame f) {
		super(f, TITLE, FileDialog.LOAD);
		config();
	}
	
	public JvGridLoader(Dialog d) {
		super(d, TITLE, FileDialog.LOAD);
		config();
	}
	
	
	public JvGridLoader(Frame parent, int numGridPointsX, int numGridPointsY) {
		super(parent, TITLE, FileDialog.LOAD);
		this.numGridPointsX = numGridPointsX;
		this.numGridPointsY = numGridPointsY;
	}

	public JvGridLoader(Dialog parent, int numGridPointsX, int numGridPointsY) {
		super(parent, TITLE, FileDialog.LOAD);
		this.numGridPointsX = numGridPointsX;
		this.numGridPointsY = numGridPointsY;
	}

	private void config(){
		setMultipleMode(false);
		setFilenameFilter(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				if(name.toLowerCase().endsWith(".jpg") ||
		                name.toLowerCase().endsWith(".jpeg") ||
		                name.toLowerCase().endsWith(".gif") ||
		                name.toLowerCase().endsWith(".png")){
					return true;
			}
			return false;
			}
		});
	}
	
	
	public JvGrid loadJvGrid(){
		setVisible(true);
		BufferedImage image = null;
		if (getFile() != null) {
			try {
				image = ImageIO.read(new File(getDirectory(), getFile()));
			} catch (IOException e) {
				System.err.println("can't load image");
				return null;
			}
			
		}else{
			return null;
		}
		name = getFile();
		JvGrid newGrid =new JvGrid(numGridPointsX, numGridPointsX, image);
		newGrid.setName(name);
		return newGrid;
	}
	
	
	
	
	
	
	
	/**
	 * @return the numGridPointsX
	 */
	public int getNumGridPointsX() {
		return numGridPointsX;
	}
	/**
	 * @param numGridPointsX the numGridPointsX to set
	 */
	public void setNumGridPointsX(int numGridPointsX) {
		this.numGridPointsX = numGridPointsX;
	}
	/**
	 * @return the numGridPointsY
	 */
	public int getNumGridPointsY() {
		return numGridPointsY;
	}
	/**
	 * @param numGridPointsY the numGridPointsY to set
	 */
	public void setNumGridPointsY(int numGridPointsY) {
		this.numGridPointsY = numGridPointsY;
	}

}
